<?php
/**
 * QFrameWork
 *
 * @author: quyang
 * @date: 2010-1-17
 * @version: 1.0
 */



function __autoload($class_name)
{
	$class_name = strtolower($class_name);
    $file = CORE_LIB_PATH.$class_name.EXT;
    if(is_file($file)){
        require_once($file);
/*
    } else {
        $prefix = substr($class_name , strpos($class_name , '_'));
		switch ($prefix) {
			case "mod":
				require_once(CORE_MODEL.$class_name.EXT);
				break;
			case "lib":
				require_once(CORE_LIB_PATH.$class_name.EXT);
				break;
			case "hlp":
				require_once(CORE_HELPER_PATH.$class_name.EXT);
				break;
			default:
				throw new SystemError('load inexistent class:'.$class_name);
				break;
		}
*/
    }
	return 0;
}

set_error_handler('errorHandler');
set_exception_handler('exceptionHandler');

function exceptionHandler($oE){
	$sExceptionType = get_class($oE);
	$error = Loader::load_Lib('error');
	switch($sExceptionType){	
		case "NotFound404":
			exit($error->show_404($oE->getMessage()));			
		case "SystemBusy":
			exit($error->show_error("System is busy now.",$oE->getMessage()));			
		case "SystemError":
			exit($error->show_error("System halted.","Here is more message:".$oE->getMessage()));
		case "ErrorOutput":
			//TODO
			$oE->errorOutput();
			break;
		default:
			exit($error->show_error("Unknown exception.","It seems we got a undefined Exception with ErrCode:[".$oE->getCode()."],ErrMsg:".$oE->getMessage()));
	}
}

/**
 * 手工处理PHP错误的显示
 * 
 * @param string $severity
 * @param string $message
 * @param string $filepath
 * @param int $line
 * @return void
 */
function errorHandler($severity, $message, $filepath, $line)
{
	$error = Loader::load_Lib('error');

	// Should we display the error?
	// We'll get the current error_reporting level and add its bits
	// with the severity bits to find out.
	
	if (($severity & error_reporting()) == $severity){
		$error->show_php_error($severity, $message, $filepath, $line);
	}
}

Class NotFound404 extends Exception {}
Class SystemBusy extends Exception {}
Class SystemError extends Exception {}
Class ErrorOutput extends Exception {
	public function __construct($message = null, $code = 0){
		parent::__construct($message, $code);
	}
	public function errorOutput(){
		Core::error_output($this->message,$this->code);
	}
}

?>